package cn.jhc.Adapter;

import java.util.List;

import com.example.shilehui.R;
import com.example.shilehui.SelectCityActivity;

import cn.jhc.Adapter.SelectProvincesAdapter.DataWrapper;
import cn.jhc.bean.City;
import cn.jhc.bean.Province;
import cn.jhc.utils.ActivityForResultRequestCode;
import cn.jhc.utils.Strings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SelectCityAdapter extends BaseAdapter {

	private Province province;
	private List<City> citys;
	private Context context;
	private LayoutInflater inflater;
	private Activity selectCityActivity;
	public SelectCityAdapter(Activity selectCityActivity,Context context,Province province, List<City> citys) {
		this.selectCityActivity= selectCityActivity;
		this.province=province;
		this.citys=citys;
		this.context=context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);	
	}

	@Override
	public int getCount() {
		return citys.size();
	}

	@Override
	public Object getItem(int position) {
		return citys.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	DataWrapper dataWrapper;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView=inflater.inflate(R.layout.select_city_listview_item, null);
			TextView city=(TextView) convertView.findViewById(R.id.select_city_listview_item_city);
			dataWrapper=new DataWrapper(city);
			convertView.setTag(dataWrapper);
		}else{
			dataWrapper=(DataWrapper) convertView.getTag();
		}
		
		dataWrapper.city.setText(citys.get(position).getCityName());
		dataWrapper.city.setOnClickListener(new ConvertViewLister(province,citys.get(position)));
		return convertView;
		
	}
	public final class DataWrapper {
		public TextView city;

		public DataWrapper(TextView city) {
			this.city=city;
		}
	}
	
	private class ConvertViewLister implements OnClickListener {

		private Province province;
		private City city;
		public ConvertViewLister(Province province,City city) {
			this.province = province;
			this.city=city;
		}

		@Override
		public void onClick(View v) {
			Intent intent = selectCityActivity.getIntent();
			intent.putExtra(Strings.SELECT_PROVINCE,province);
			intent.putExtra(Strings.SELECT_CITY,city);
			intent.putExtra(Strings.SELECT_FINISH, true);
		
			selectCityActivity.setResult(ActivityForResultRequestCode.SELECT_CITY, intent);
			selectCityActivity.finish();
		}

	}
}
